layui.use(['table', 'jquery'], function () {
    let table = layui.table
        , $ = layui.jquery
        , layer = layui.layer
        , form = layui.form;
    //展示选题表数据
    table.render({
        elem: '#paper-info'
        , toolbar: true
        , title: '论文信息表'
        , url: '/getPaper'
        , page: true
        , where: {userName: sno, state: "任务书", type: 1}
        , even: true
        , cols: [[ //标题栏
            {field: 'bh', title: '编号', sort: true, hide: true}
            , {field: 'title', title: '课题名称'}
            , {field: 'cid', title: '所属学院'}
            , {field: 'major', title: '专业',templet:function (res){
                    return res.title1.majorName;
                }}
            , {field: 'sname', title: '学生姓名'}
            , {field: 'sno', title: '学生学号'}
            , {
                field: 'tno1', title: '教师姓名', templet: function (res) {
                    return res.title1.tName;
                }
            }
            , {field: 'tno', title: '教师工号'}
            , {field: 'state', title: '论文阶段'}
            , {fixed: 'right', title: '课题详情', toolbar: '#title-tool', width: 100}
        ]]
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });
    let flag = true;
    $.ajax({
        url: "/titleInfo",
        type: 'post',
        data: {sno: sno, page: 1, limit: 100},
        dataType: 'json',
        success: function (json) {
            for (let i = 0; i < json.count; i++) {
                if (json.data[i].state === "审核中") {
                    flag = false;
                }
            }
        }
    })
    $.ajax({
        url: "/getPaper",
        type: 'post',
        data: {
            userName: sno, state: "任务书", type: 1, page: 1, limit: 100
        },
        dataType: 'json',
        success: function (json) {
            if (json.count != 0) {
                flag = false;
            }
        }
    })
    //绑定选择课题按钮
    $('#title-sel-btn').click(function () {
        if (flag === true) {
            let page = layer.open({
                type: 1
                , offset: 10
                , area: [1100, 650]
                , content: $("#choose-task-box")
                , success: function () {
                }
                , end: function () {
                    $("#choose-task-box").css("display", 'none');
                }
            });
        } else {
            layer.msg("你已申请课题，不可多次申请")
        }
    });

    //监听行工具事件
    table.on('tool(paper-info)', function (obj) {
        let data = obj.data;
        if (obj.event === 'taskDetail') {  //查看课题详情
            layer.open({
                type: 1
                , offset: 50
                , shade: [0.3, '#000']
                , shadeClose: true
                , skin: 'layui-layer-rim'
                , area: ['740px', '600px']
                , content: $("#task-detail")
                , title: '学生课题详情内容'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    /*设置回显数据*/
                    setTaskDetail(data);
                }
                , end: function () {
                    $("#task-detail").css("display", 'none');
                }
            });
        }
    });
    //监听搜索按钮
    form.on('submit(sel)', function (data) {
        console.log(data.field); //当前容器的全部表单字段，名值对形式：{name: value}
        let taskData = JSON.stringify(data.field);
        table.reload('choose-task', {
            where: {sno: sno, taskData: taskData}
        });
        return false;//阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });


    //盒子内数据表
    table.render({
        elem: '#choose-task',
        toolbar: true,
        title: '盒子内选题信息表',
        url: '/titleSelect',
        where: {sno: sno},
        page: true,
        even: true,
        height: 400,
        cols: [
            [
                {field: 'xt_id', title: '编号', sort: true, hide: true, width: 150}
                , {field: 'title', title: '课题名称', width: 150}
                , {field: 'collegeName', title: '学院', width: 150}
                , {field: 'majorName', title: '专业', width: 150}
                , {field: 'tName', title: '发布教师', width: 150}
                , {field: 'tno', title: '教师工号', width: 150}
                , {fixed: 'right', title: '课题详情', toolbar: '#title-tool', width: 100}
                , {fixed: 'right', title: '操作', toolbar: '#choose-task-tool', width: 200}

            ]
        ]
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });
    //监听盒子内行工具事件
    table.on('tool(choose-task)', function (obj) {
        let data = obj.data;
        if (obj.event === 'confirm') {
            data = JSON.stringify(data);//转换为json字符串
            layer.confirm('确认选择？', function () {
                let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
                $.ajax({
                    url: "/studentAddPaper",
                    type: 'post',
                    data: {
                        taskData: data, userName: sno
                    },
                    dataType: 'json',
                    success: function (json) {  //调用接口成功
                        if (json === true || json === 'true') {
                            layer.close(loadPage);
                            layer.closeAll('page');
                            layui.layer.msg('申请成功！', {offset: '300px', time: 2500, anim: 0});
                            table.reload('title-info', {
                                url: '/titleSelect',
                                where: {sno: sno, taskData: data}
                            });
                        } else {
                            layui.layer.close(loadPage);
                            layui.layer.alert('申请失败！');
                        }
                    },
                    error: function () {  //接口错误
                        layui.layer.close(loadPage);
                        layui.layer.alert('服务器出错啦,请稍后再试！');
                    }
                });
            });
        } else if (obj.event === 'teacher-info') {
            layer.open({
                title: '教师信息'
                , type: 1
                , shadeClose: true
                , offset: 50
                , area: ['740px', '400px']
                , content: $('#teacher-detail')
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());//解决弹窗点不到屏幕变暗的问题
                    $.ajax({
                        url: "/getTeacher",
                        type: 'post',
                        data: {
                            tNO: data.tno
                        },
                        dataType: 'json',
                        success: function (json) {
                            $("#teacher-tname").html(data.tName);
                            $("#teacher-tno").html(data.tno);
                            $("#teacher-college").html(data.collegeName);
                            $("#teacher-sex").html(json.data[0].sex);
                            $("#teacher-post").html(json.data[0].post);
                            $("#teacher-department").html(json.data[0].department);
                        }
                    })
                }
                , end: function () {
                    $('#teacher-detail').css("display", 'none')
                }
            });
        } else if (obj.event === 'query') {
            layer.open({
                type: 1
                , offset: 50
                , skin: 'layui-layer-rim'
                , area: ['740px', '600px']
                , content: $("#task-detail")
                , title: '课题详情内容'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());//解决弹窗点不到屏幕变暗的问题
                    setTaskDetail(data);
                }
                , end: function () {
                    $("#task-detail").css("display", 'none');
                }
            });
        }
    });

    //设置课题详情
    function setTaskDetail(data) {
        $("#task-tno").html(data.title1.tno);
        $("#task-sno").html(data.sno);
        $("#task-sname").html(data.sname);
        $("#task-from").html(data.title1.from);
        $("#task-id").html(data.title1.xtId);
        $("#task-title").html(data.title);
        $("#task-category").html(data.title1.categoty);
        $("#task-addtime").html(data.title1.addtime);
        $("#task-nature").html(data.title1.nature);
        $("#task-major").html(data.title1.majorName);
        $("#task-tname").html(data.title1.tName);
        $("#task-state").html(data.title1.state);
        $("#task-intr").html(data.title1.intr);
        $("#task-college").html(data.title1.collegeName);
    }

});